package cn.buaa.beijing;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import java.util.ArrayList;

import cn.buaa.beijing.util.PrefUtil;

public class GuideActivity extends Activity {

    private static final int[] mImageIds = new int[] { R.drawable.guide_1,
            R.drawable.guide_2, R.drawable.guide_3 };

    private ViewPager vpGuide;
    private ArrayList<ImageView> mImageViewList;
    private LinearLayout llPointGroup;
    private View viewRedPoint;
    private Button btn_start;

    private int mPointWidth;// 圆点间的距离
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        vpGuide = (ViewPager) findViewById(R.id.vp_guide);
        llPointGroup = (LinearLayout) findViewById(R.id.ll_point_group);
        viewRedPoint = findViewById(R.id.view_red_point);
        btn_start = (Button) findViewById(R.id.btn_start);
        btn_start.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                PrefUtil.setBoolean(GuideActivity.this,"lookedGuide",true);
                startActivity(new Intent(GuideActivity.this, MainActivity.class));
                finish();
            }
        });
        initView();
        vpGuide.setAdapter(new GuideAdapter());
        //使用ViewPager发现setOnPageChangeListener的方法居然过期了，
        // 而且AS编译不通过了，最后查了一下原来把set换成add了
        vpGuide.addOnPageChangeListener(new PageChange());
    }

    private void initView() {
        mImageViewList = new ArrayList<ImageView>();
        for (int i = 0; i < mImageIds.length ; i++) {
            ImageView image = new ImageView(this);
            image.setImageResource(mImageIds[i]);
            mImageViewList.add(image);
        }
        // 初始化引导页的小圆点
        for (int i = 0; i < mImageIds.length; i++) {
            View point = new View(this);
            point.setBackgroundResource(R.drawable.shape_point_gray);// 设置引导页默认圆点

            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    10, 10);
            if (i > 0) {
                params.leftMargin = 20;// 设置圆点间隔
            }

            point.setLayoutParams(params);// 设置圆点的大小


            llPointGroup.addView(point);// 将圆点添加给线性布局
            llPointGroup.getViewTreeObserver().addOnGlobalLayoutListener(
                    new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    //避免该方法重复调用
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                        llPointGroup.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    }
                    //llPointGroup.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                    mPointWidth = llPointGroup.getChildAt(1).getLeft()
                            - llPointGroup.getChildAt(0).getLeft();
                }
            });
        }
    }

    class GuideAdapter extends PagerAdapter{

        @Override
        public int getCount() {
            return mImageIds.length;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(mImageViewList.get(position));
            return mImageViewList.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }
    class PageChange implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) viewRedPoint.getLayoutParams();
            params.leftMargin = (int)(mPointWidth*position + positionOffset*mPointWidth);
            viewRedPoint.setLayoutParams(params);
        }

        @Override
        public void onPageSelected(int position) {
            if(position==(mImageIds.length-1)){
                btn_start.setVisibility(View.VISIBLE);
            }else{
                btn_start.setVisibility(View.INVISIBLE);
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }
}
